
package ucm.module;

import air_cp5620.dao.DAO;
import javax.swing.JOptionPane;
import java.sql.*;


public class UC_RegM {

    private DAO dbCon = null;
    private String userName = null;
    private String passWord = null;
    private String roldId = null;

    public UC_RegM(String UserName, String PassWord,String RoleId) {
        this.userName = UserName;
        this.passWord = PassWord;
        this.roldId = RoleId;
        dbCon = new DAO();
    }


    public boolean regisger() {
        String sql = null;
        Connection con = dbCon.getConnection();
        if (con == null) {
            return false;
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            sql = "SELECT * FROM tbl_user WHERE userid = '" + userName + "'";
            ResultSet res = stmt.executeQuery(sql);

            if (res.getRow() == 0) {
                sql = "INSERT INTO tbl_user (userid,password,roleid) values('" + userName + "', '" + passWord + "','" + roldId + "')";
                stmt.executeUpdate(sql);
                JOptionPane.showMessageDialog(null, "User registration is successful!");
                return true;
            }
            return false;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "The user name has been registered, please change！");
            return false;
        }
    }
}
